iT邦幫忙

2022 iThome 鐵人賽

DAY 7
0
自我挑戰組

30天Java由淺入深系列 第 7

Day 7 : Operators and Mathematical Calculations

  • 分享至 

  • xImage
  •  

#Intro

In this chapter, we will share five useful mathematical functions in Java for performing mathematical operations. However, before that, we will first explain a topic that bridges the past and the future – operators. Whether it is used in mathematical operations or the logical operators that will be introduced later for conditional judgment, operators are used to tell the compiler what to execute using symbols. This is something that is well worth knowing!!!


#Operators

  • Arithmetic Operators
    https://ithelp.ithome.com.tw/upload/images/20220922/20151216Y3A5UGMYUF.png

  • Assignment operators
    https://ithelp.ithome.com.tw/upload/images/20220922/20151216FmpOO8uadc.png

  • Comparison operators
    https://ithelp.ithome.com.tw/upload/images/20220922/20151216t1Sc3Rt3QF.png

  • Logical operators
    https://ithelp.ithome.com.tw/upload/images/20220922/20151216fxeNHn07Pf.png

  • Bitwise operators
    https://ithelp.ithome.com.tw/upload/images/20220922/201512164cWZO1BpRa.png


#Mathematical Calculations

The following describes the application of mathematical functions and provides program examples to complement the content of the above operators:

  1. Math.max( )
    Finds the maximum value between two numbers. Put the two numbers in the middle of the brackets and separate them with a comma.
  2. Math.min( )
    Finds the minimum value between two numbers. Put the two numbers in the middle of the brackets and separate them with a comma.
public class Main(){
	public static void main(String[] agrs){
		int x = 10, y = 11;
		int max,min;
		max = Math.max(x,y);         //max = 11 
		min = Math.min(x,y);         //min = 10

		max -= 1;           //max = 10
		min++;              //min = 11
		if(max == min) System.out.println("Hello!");
			else if(max > min) System.out.println("Hola!!");
				else System.out.println("你好!!!")        //Outputs : 你好!!!
	} 
}
  1. Math.sqrt( )
    Finds the square root of a number. Enter a number.
  2. Math.abs( )
    Converts a negative number to a positive number. The concept of absolute value. Enter a negative number.
public class Main(){
	public static void main(String[] args){
		int root;
		root = Math.sqrt(100);       //Outputs : 10
		root /= -10;
		Syestem.out.println(Math.abs(root));    //Outputs : 1
	}
}
  1. Math.random( )
    A random decimal between 0.0 (inclusive) and 1.0 (exclusive) is generated.
    → Supplement: Because we may need to use a number or set of numbers between 0 and 100 when executing the program, but this function can only produce a very small decimal, we can multiply it by 101 (up to 100), and the result will be a decimal with one digit!
public class Main(){
	public static void main(String[] args){
		int random;
		random = (int)(Math.random() * 101 );
		System.out.println(random);    //Outputs : random number between 0~100
	}
}
  • Program analysis: We can see that there is a special usage in line 4 (int) This is to ensure that the random variable generated later is a decimal point, and to make it without a decimal point, we use the method of “forced conversion”. Forced conversion involves type conversion between variables, and the following provides a casting sequence between variables.
    From small to large, a forced conversion is required for small to large, while no conversion is required for large to small.

    byte-> short-> char-> int-> long-> float-> double

/images/emoticon/emoticon35.gif


上一篇
Day 6 : Variables( 2 )
下一篇
Day 8 : Conditional Expressions
系列文
30天Java由淺入深30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言